package com.bt.lms.dao.servise.impl;

import com.bt.lms.constant.BOConstant;
import com.bt.lms.entity.JdbcUtils;
import com.bt.lms.entity.TeacherBasicInfo;
import com.bt.lms.dao.servise.TeacherBasicInfoDao;

import java.sql.*;
import java.time.LocalDate;

/**
 * 教师管理 dao接口实现
 * @author 李舒淇
 */
public class TeacherBasicInfoDaoImpl implements TeacherBasicInfoDao {


    @Override
    public int insert(String sql, TeacherBasicInfo teacherBasicInfo) throws Exception {

        teacherBasicInfo.setOnJobStatus(BOConstant.TEACHER_BASIC_INFO_ON_JOB_STATUS_ON_JOD);


        String account = teacherBasicInfo.getAccount();//账号
        String name = teacherBasicInfo.getName();//姓名
        Integer certificateType = teacherBasicInfo.getCertificateType();//证件类型
        String certificateNumber = teacherBasicInfo.getCertificateNumber();//证件号码
        Integer gender = teacherBasicInfo.getGender();//性别
        LocalDate birthdate = teacherBasicInfo.getBirthdate();//出生日期
        String birthplace = teacherBasicInfo.getBirthplace();//籍贯
        String nation = teacherBasicInfo.getNation();//民族
        Integer politicsStatus = teacherBasicInfo.getPoliticsStatus();//政治面貌
        Integer affiliationUnit = teacherBasicInfo.getLearningCenter();//所属的学习中心
        String specialty = teacherBasicInfo.getSpecialty();//专业
        String jobTitle = teacherBasicInfo.getJobTitle();//职称
        Integer highestDegree = teacherBasicInfo.getHighestDegree();//最高学历
        Integer teachingAge = teacherBasicInfo.getTeachingAge();//教龄
        String phone = teacherBasicInfo.getPhone();//联系电话
        String researchInterests = teacherBasicInfo.getResearchInterests();//备注
        String picture = teacherBasicInfo.getPicture();//照片
        Integer onJobStatus = teacherBasicInfo.getOnJobStatus();//在职状态
//        Integer enableStatus = teacherBasicInfo.getEnableStatus();//启用状态


        Connection jdbc = JdbcUtils.jdbc();
        PreparedStatement pS = jdbc.prepareStatement(sql);


        pS.setString(1,account);
        pS.setString(2,name);
        pS.setInt(3,certificateType);
        pS.setString(4,certificateNumber);
        pS.setInt(5,gender);
        pS.setDate(6, Date.valueOf(birthdate));
        pS.setString(7,birthplace);
        pS.setString(8,nation);
        pS.setInt(9,politicsStatus);
        pS.setInt(10,affiliationUnit);
        pS.setString(11,specialty);
        pS.setString(12,jobTitle);
        pS.setInt(13,highestDegree);
        pS.setInt(14,teachingAge);
        pS.setString(15,phone);
        pS.setString(16,researchInterests);
        pS.setString(17,picture);
        pS.setInt(18,onJobStatus);
//        pS.setInt(19,enableStatus);

        //4.执行sql语句
        int i = pS.executeUpdate();

        //5.关闭资源
        pS.close();
        jdbc.close();


        return 0;
    }


    @Override
    public TeacherBasicInfo details(String sql,TeacherBasicInfo teacherBasicInfo,Long id) throws Exception {

        Connection jdbc = JdbcUtils.jdbc();
        PreparedStatement pS = jdbc.prepareStatement(sql);
        pS.setLong(1,id);

        ResultSet resultSet = pS.executeQuery();
        boolean next = resultSet.next();
        if (next){
            //获取数据库的内容
            String account = resultSet.getString("account");
            String name = resultSet.getString("name");
            int certificateType = resultSet.getInt("certificate_Type");
            String certificateNumber = resultSet.getString("certificate_Number");
            int gender = resultSet.getInt("gender");
            Date birthdate = resultSet.getDate("birthdate");
            String birthplace = resultSet.getString("birthplace");
            String nation = resultSet.getString("nation");
            int politicsStatus = resultSet.getInt("politics_Status");
            int learningCenter = resultSet.getInt("Learning_Center");
            String specialty = resultSet.getString("specialty");
            String jobTitle = resultSet.getString("job_Title");
            int highestDegree = resultSet.getInt("highest_Degree");
            int teachingAge = resultSet.getInt("teaching_Age");
            String phone = resultSet.getString("phone");
            String researchInterests = resultSet.getString("research_Interests");
            String picture = resultSet.getString("picture");

            //带走
            teacherBasicInfo.setAccount(account);
            teacherBasicInfo.setName(name);
            teacherBasicInfo.setCertificateType(certificateType);
            teacherBasicInfo.setCertificateNumber(certificateNumber);
            teacherBasicInfo.setGender(gender);
            teacherBasicInfo.setBirthdate(birthdate.toLocalDate());
            teacherBasicInfo.setBirthplace(birthplace);
            teacherBasicInfo.setNation(nation);
            teacherBasicInfo.setPoliticsStatus(politicsStatus);
            teacherBasicInfo.setLearningCenter(learningCenter);
            teacherBasicInfo.setSpecialty(specialty);
            teacherBasicInfo.setJobTitle(jobTitle);
            teacherBasicInfo.setHighestDegree(highestDegree);
            teacherBasicInfo.setTeachingAge(teachingAge);
            teacherBasicInfo.setPhone(phone);
            teacherBasicInfo.setResearchInterests(researchInterests);
            teacherBasicInfo.setPicture(picture);


        }
        return teacherBasicInfo;
    }


    @Override
    public boolean edit(String sql, TeacherBasicInfo teacherBasicInfo,Long id) throws Exception {

        String account = teacherBasicInfo.getAccount();//账号
        String name = teacherBasicInfo.getName();//姓名
        Integer certificateType = teacherBasicInfo.getCertificateType();//证件类型
        String certificateNumber = teacherBasicInfo.getCertificateNumber();//证件号码
        Integer gender = teacherBasicInfo.getGender();//性别
        LocalDate birthdate = teacherBasicInfo.getBirthdate();//出生日期
        String birthplace = teacherBasicInfo.getBirthplace();//籍贯
        String nation = teacherBasicInfo.getNation();//民族
        Integer politicsStatus = teacherBasicInfo.getPoliticsStatus();//政治面貌
        Integer learningCenter = teacherBasicInfo.getLearningCenter();//所属的学习中心
        String specialty = teacherBasicInfo.getSpecialty();//专业
        String jobTitle = teacherBasicInfo.getJobTitle();//职称
        Integer highestDegree = teacherBasicInfo.getHighestDegree();//最高学历
        Integer teachingAge = teacherBasicInfo.getTeachingAge();//教龄
        String phone = teacherBasicInfo.getPhone();//联系电话
        String researchInterests = teacherBasicInfo.getResearchInterests();//备注
        String picture = teacherBasicInfo.getPicture();//照片
//        Integer onJobStatus = teacherBasicInfo.getOnJobStatus();//在职状态
//        Integer enableStatus = teacherBasicInfo.getEnableStatus();//启用状态



        Connection jdbc = JdbcUtils.jdbc();
        PreparedStatement pS = jdbc.prepareStatement(sql);
        pS.setLong(18,id);

        pS.setString(1,account);
        pS.setString(2,name);
        pS.setInt(3,certificateType);
        pS.setString(4,certificateNumber);
        pS.setInt(5,gender);
        pS.setDate(6, Date.valueOf(birthdate));
        pS.setString(7,birthplace);
        pS.setString(8,nation);
        pS.setInt(9,politicsStatus);
        pS.setInt(10,learningCenter);
        pS.setString(11,specialty);
        pS.setString(12,jobTitle);
        pS.setInt(13,highestDegree);
        pS.setInt(14,teachingAge);
        pS.setString(15,phone);
        pS.setString(16,researchInterests);
        pS.setString(17,picture);
//        pS.setInt(18,onJobStatus);
//        pS.setInt(19,enableStatus);

        //4.执行sql语句
        int i = pS.executeUpdate();

        //5.关闭资源
        pS.close();
        jdbc.close();


        return false;
    }

    @Override
    public boolean enable(String sql,Long id) throws Exception {

        Connection jdbc = JdbcUtils.jdbc();
        PreparedStatement pS = jdbc.prepareStatement(sql);
        pS.setInt(1, BOConstant.TEACHER_BASIC_INFO_ENABLE_ENABLE);
        pS.setLong(2,id);
        //4.执行sql语句
        int i = pS.executeUpdate();


        //5.关闭资源
        pS.close();
        jdbc.close();
        return false;
    }

    @Override
    public boolean stop(String sql,Long id) throws Exception {

        Connection jdbc = JdbcUtils.jdbc();
        PreparedStatement pS = jdbc.prepareStatement(sql);
        pS.setInt(1, BOConstant.TEACHER_BASIC_INFO_ENABLE_STATUS_BLOCK_UP);
        pS.setLong(2,id);
        //4.执行sql语句
        int i = pS.executeUpdate();


        //5.关闭资源
        pS.close();
        jdbc.close();
        return false;
    }

    @Override
    public boolean onJob(String sql,Long id) throws Exception {

        Connection jdbc = JdbcUtils.jdbc();
        PreparedStatement pS = jdbc.prepareStatement(sql);
        pS.setInt(1, BOConstant.TEACHER_BASIC_INFO_ON_JOB_STATUS_ON_JOD);
        pS.setLong(2,id);
        //4.执行sql语句
        int i = pS.executeUpdate();


        //5.关闭资源
        pS.close();
        jdbc.close();
        return false;
    }

    @Override
    public boolean leaveOffice(String sql,Long id) throws Exception {

        Connection jdbc = JdbcUtils.jdbc();
        PreparedStatement pS = jdbc.prepareStatement(sql);
        pS.setInt(1, BOConstant.TEACHER_BASIC_INFO_ON_JOB_STATUS_LEAVE_OFFICE);
        pS.setInt(2, BOConstant.TEACHER_BASIC_INFO_ENABLE_STATUS_BLOCK_UP);
        pS.setLong(3,id);
        //4.执行sql语句
        int i = pS.executeUpdate();


        //5.关闭资源
        pS.close();
        jdbc.close();
        return false;
    }
}
